package com.currentcms.client;

import java.util.ArrayList;
import java.util.List;

import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.DOM;
import com.google.gwt.user.client.ui.DialogBox;

public class Util
{

	public static void setDefaultExceptionHandler()
	{
		// set uncaught exception handler
		GWT.setUncaughtExceptionHandler(new GWT.UncaughtExceptionHandler()
		{
			public void onUncaughtException(Throwable throwable)
			{
				String text = "Uncaught exception: ";
				while (throwable != null)
				{
					StackTraceElement[] stackTraceElements = throwable.getStackTrace();
					text += throwable.toString() + "\n";
					for (int i = 0; i < stackTraceElements.length; i++)
					{
						text += "    at " + stackTraceElements[i] + "\n";
					}
					throwable = throwable.getCause();
					if (throwable != null)
					{
						text += "Caused by: ";
					}
				}
				DialogBox dialogBox = new DialogBox(true);
				DOM.setStyleAttribute(dialogBox.getElement(), "backgroundColor", "#ABCDEF");
				System.err.print(text);
				text = text.replaceAll(" ", "&nbsp;");
				dialogBox.setHTML("<pre>" + text + "</pre>");
				dialogBox.center();
			}
		});
	}
	
	
	public static <I> List<I> L(I... objects)
	{
		List<I> o = new ArrayList<I>();
		for (int i = 0; i < objects.length; i++)
			o.add(objects[i]);
		return o;
	}

}
